﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ZombieMonkeys.Vibria.Logic;

namespace ZombieMonkeys.Vibria.DAL
{
    /// <summary>
    /// A class that will read in an xml file and load the data as a sudoku puzzle
    /// </summary>
    public class PuzzleLoader
    {
        public Puzzle Load(string Id)
        {
            string path = @"..\..\..\..\DataLayer\SudokunatortronData.xml";
            int i = 0;
            int j = 0;
            List<Square> squares = new List<Square>();
            XDocument doc = XDocument.Load(path);
            string elements = string.Empty;
            foreach (XElement element in doc.Root.Elements())
            {
                if (element.FirstAttribute.Value == Id)
                {
                    elements += element.Value;
                }
            }
            foreach (string value in elements.Split(','))
            {
                Square square = new Square();
                square.SetRow(i);
                square.SetColumn(j);
                square.SetPoint();
                square.SetValue(int.Parse(value));
                squares.Add(square);
                if (i != 8)
                {
                    i++;
                }
                else
                {
                    i = 0;
                    j++;
                }
            }
            return new Puzzle(new Grid(squares));

        }
    }
}
